package com.jzo2o.customer.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.BankAccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author wanfeng
 * @version 1.0
 * @since 2025-09-17 20:55
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements BankAccountService {

    @Resource
    private BankAccountMapper bankAccountMapper;

    /**
     * 银行账号开通或更改
     * @param bankAccountUpsertReqDTO
     */
    @Override
    @Transactional
    public void bankAccountUpsert(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {

        if(ObjectUtil.isNull(bankAccountUpsertReqDTO.getId())){
            BankAccount bankAccount = BeanUtil.toBean(bankAccountUpsertReqDTO, BankAccount.class);
            //id为空就新增
            bankAccount.setId(UserContext.getCurrentUser().getId());
            bankAccountMapper.insert(bankAccount);
        } else {
            // 更新前检查记录是否存在
            BankAccount existing = bankAccountMapper.selectById(bankAccountUpsertReqDTO.getId());
            if (existing == null) {
                throw new RuntimeException("要更新的记录不存在");
            }
            BankAccount updateEntity = BeanUtil.toBean(bankAccountUpsertReqDTO, BankAccount.class);
            //id不为空就更新
            updateEntity.setName(bankAccountUpsertReqDTO.getName());
            updateEntity.setAccount(bankAccountUpsertReqDTO.getAccount());
            updateEntity.setAccountCertification(bankAccountUpsertReqDTO.getAccountCertification());
            updateEntity.setDistrict(bankAccountUpsertReqDTO.getDistrict());
            updateEntity.setType(bankAccountUpsertReqDTO.getType());
            updateEntity.setProvince(bankAccountUpsertReqDTO.getProvince());
            updateEntity.setBranch(bankAccountUpsertReqDTO.getBranch());
            updateEntity.setBankName(bankAccountUpsertReqDTO.getBankName());
            updateEntity.setCity(bankAccountUpsertReqDTO.getCity());
            updateEntity.setUpdateTime(LocalDateTime.now());
            bankAccountMapper.updateById(updateEntity);
        }

    }

    /**
     * 获取银行账户信息
     * @return
     */
    @Override
    public BankAccountResDTO getAccount() {
        // 使用查询列表方法，按创建时间倒序并限制一条记录
        LambdaQueryWrapper<BankAccount> queryWrapper = new LambdaQueryWrapper<BankAccount>()
                .eq(BankAccount::getId, UserContext.getCurrentUser().getId())
                .orderByDesc(BankAccount::getCreateTime)
                .last("LIMIT 1");

        BankAccount bankAccount = bankAccountMapper.selectOne(queryWrapper);
        BankAccountResDTO bankAccountResDTO = BeanUtil.toBean(bankAccount, BankAccountResDTO.class);
        return bankAccountResDTO;
    }
}
